/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zzz.view.managerControl;

import javax.swing.*;

import org.zzz.controller.CourseController;
import org.zzz.controller.TeacherController;
import org.zzz.controller.TeacherCourseController;
import org.zzz.entity.Course;
import org.zzz.entity.Teacher;
import org.zzz.entity.TeacherCourse;
import org.zzz.entity.TeacherVo;
import org.zzz.entity.vo.CourseShowVo;
import org.zzz.utils.Result;
import org.zzz.utils.ResultEnum;
import org.zzz.view.util.UiUtil;

import java.util.List;
import java.util.UUID;

/**
 *
 * @author AFun
 */
public class managerCosAlter extends javax.swing.JFrame {

    /**
     * Creates new form managerCosAlter
     */
    public managerCosAlter(String CouNum,String CouName,String CouTeacher) {
        initComponents();
        init(CouNum,CouName,CouTeacher);         //初始化
    }

    private void init(String CouNum,String CouName,String CouTeacher) {
        this.setTitle("课程修改");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
        this.kechenghao.setText(CouNum);
        this.kechengming.setText(CouName);
        //获取全部教师名
        TeacherController teacherController = new TeacherController();
        TeacherCourseController teacherCourseController = new TeacherCourseController();
        Result<TeacherCourse> teacherCourseResult = teacherCourseController.getTCByCid(this.kechenghao.getText());
        Result<List<Teacher>> listResult = teacherController.listAllTeacher();
        List<Teacher> teachers = listResult.getData();
        int index=0;
        for (Teacher datum : teachers) {
            this.cTeacher.insertItemAt(new Teacher(datum.getId(),datum.getName(),datum.getGender(),datum.getDegree()){
                @Override
                public String toString() {
                    return datum.getName();
                }
            },index++);
        }


        //设置教师
        TeacherCourse teacherCourse = teacherCourseResult.getData();
        if (teacherCourse != null){
            for(int i=0;i<this.cTeacher.getItemCount();i+=1){
                if(this.cTeacher.getItemAt(i).getId().equals(teacherCourse.getTid())){

                    this.cTeacher.setSelectedIndex(i);
                    break;
                }
            }
        }


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        kechenghao = new javax.swing.JTextField();
        kechengming = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        cTeacher = new javax.swing.JComboBox<>();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        kechenghao.setEditable(false);
        jLabel5.setText("课程号");

        jLabel6.setText("课程名");

        jLabel7.setText("任课教师");

        jButton3.setText("确定");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("取消");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel5)
                                        .addComponent(jLabel7))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(18, 18, 18)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(kechengming, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(kechenghao, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGap(250, 250, 250)
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING)
                                                                        .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING)))))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(29, 29, 29)
                                                .addComponent(cTeacher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel5)
                                                        .addComponent(kechenghao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(27, 27, 27)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel6)
                                                        .addComponent(kechengming, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(24, 24, 24)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel7)
                                                        .addComponent(cTeacher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(72, 72, 72))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jButton3)
                                                .addGap(16, 16, 16)
                                                .addComponent(jButton4)))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //修改
        // TODO add your handling code here:
        CourseController courseController = new CourseController();
        TeacherCourseController teacherCourseController = new TeacherCourseController();
        String courseId = this.kechenghao.getText();
        String courseName = this.kechengming.getText();
        Teacher selectedItem = (Teacher)this.cTeacher.getSelectedItem();
        courseController.updateCourse(new Course(courseId,courseName));
        if (selectedItem != null) {
            String tid = selectedItem.getId();
            Result<TeacherCourse> tcResult = teacherCourseController.getTCByCid(courseId);
            if (tcResult.getStatus() == ResultEnum.SUCCESS.getStatus()) {
                TeacherCourse tc = tcResult.getData();
                teacherCourseController.updateTC(new TeacherCourse(tc.getId(), tid, courseId));
            } else {
                teacherCourseController.insertTC(new TeacherCourse(UUID.randomUUID().toString(), tid, courseId));
            }
        }
        JOptionPane.showMessageDialog(this,"修改成功！");
        manageCouSelect mcs = new manageCouSelect();
        this.dispose();
        mcs.setVisible(true);




//        if (JOptionPane.showConfirmDialog(null, "是否修改？", "Message", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
//            //是：将文本框清空
//        } else {
//            //回到上一个界面
//            manageCouSelect mcs = new manageCouSelect();
//            this.dispose();
//            mcs.setVisible(true);
//        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        manageCouSelect mcs = new manageCouSelect();
        this.dispose();
        mcs.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(managerCosAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(managerCosAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(managerCosAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(managerCosAlter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new managerCosAlter("","","").setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JComboBox<Teacher> cTeacher;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField kechenghao;
    private javax.swing.JTextField kechengming;
    // End of variables declaration
}
